# -*- coding: utf-8 -*-

from fastapi import APIRouter, Depends
from starlette.websockets import WebSocketDisconnect

from apps.auth.user.utils.auth import WebsocketAuth, UserSocketAuth
from apps.ws.manager import ConnectionManager

app = APIRouter()

@app.websocket("/{client_id}")
async def websocket_endpoint(
  client_id: str,
  auth: WebsocketAuth = Depends(UserSocketAuth())
):
  connection_manager = ConnectionManager()
  await connection_manager.connect(client_id, auth.websocket, auth.user.id)
  try:
    while True:
      data = await auth.websocket.receive_text()
      print(data)
  except WebSocketDisconnect:
    await connection_manager.disconnect(client_id, auth.user.id)